package com.hcb.tonguetiphealth.widget.homepage;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.hcb.tonguetiphealth.adapter.OrderedInfoListAdapter;
import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.manager.JSONManager;
import com.hcb.tonguetiphealth.model.Ordered;
import com.hcb.tonguetiphealth.model.OrderedInfo;
import com.hcb.tonguetiphealth.util.HttpUtils;
import com.hcb.tonguetiphealth.util.MessageWhat;
import com.hcb.tonguetiphealth.util.StringUtils;
import com.hcb.tonguetiphealth.view.R;
import com.hcb.tonguetiphealth.widget.GeneralWidget;

public class HomepageOrderedInfoWidget extends GeneralWidget implements OnClickListener {
	private static final String TAG = "HomepageOrderedInfoWidget";
	private String url = Global.URL + Global.servlet_orderedInfo;
	
	private Activity ctx;
	private Handler mHandler;
	private LinearLayout ll_back;
	private TextView title, priceTotal;
	private ListView listView;
	private OrderedInfoListAdapter orderedInfoListAdapter;
	private List<OrderedInfo> orderedInfoList;
	
	private DecimalFormat df;
	private Ordered ordered;
	private int type;
	
	public HomepageOrderedInfoWidget(Context context) {
		super(context);
		ctx = (Activity)context;
		inflate(context, R.layout.homepage_personal_ordered_info, this); 
		type = ctx.getIntent().getIntExtra("type", 0);
		ordered = (Ordered)ctx.getIntent().getSerializableExtra("ordered");
		
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch(msg.what) {
				case MessageWhat.SHOW_DIALOG:
					showMessageDialog(ctx, "正在获取订单详情...");
					break;
				case MessageWhat.DIMISS_DIALOG:
					orderedInfoListAdapter.notifyDataSetChanged();
					priceTotal.setText(ordered.getPrice());
					dismissMessageDialog();
					break;
				}
			}
		};
		title = (TextView)findViewById(R.id.title_homepage_ordered_info_textview);
		priceTotal = (TextView)findViewById(R.id.homepage_ordered_info_price_total_textView);
		listView = (ListView)findViewById(R.id.homepage_ordered_info_listview);
		ll_back = (LinearLayout)findViewById(R.id.homepage_ordered_info_back_button);
		ll_back.setOnClickListener(this);

		
		initData();
	}
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.homepage_ordered_info_back_button:
			ctx.setContentView(new HomepageOrderedWidget(ctx));
			break;
		default:
			break;
		}
	}
	
	private void initData() {
		df = new DecimalFormat("#0.00");
		switch(type) {
		case Ordered.wine:
			title.setText(R.string.homepage_ordered_info_wine_title);
			break;
		case Ordered.meal:
			title.setText(R.string.homepage_ordered_info_meal_title);
			break;
		case Ordered.cate:
			title.setText(R.string.homepage_ordered_info_cate_title);
			break;
		}
		priceTotal.setText(df.format(0));
		orderedInfoList = new ArrayList<OrderedInfo>();
		orderedInfoListAdapter = new OrderedInfoListAdapter(ctx, orderedInfoList);
		listView.setAdapter(orderedInfoListAdapter);
		
		downOrderedInfo();
	}
	
	private void downOrderedInfo() {
		mHandler.sendEmptyMessage(MessageWhat.SHOW_DIALOG);
		new Thread() {
			public void run() {
				orderedInfoList.clear();
				String response = null;
				try {
					List <NameValuePair> params = new ArrayList <NameValuePair>();
					params.add(new BasicNameValuePair("orderedId", ordered.getId()));
					params.add(new BasicNameValuePair("orderedType", ordered.getType()));
					response = HttpUtils.Post(url, params);
					
					if(Global.DEBUG) {
						System.out.println("downRes -----------------------------\n" + response);
					}
				} catch(Exception e) {
					Log.e(TAG, "Exception", e);
				}
				if(!StringUtils.isEmpty(response)) {
					JSONManager jManager = new JSONManager();
					boolean isGotJSONObject = jManager.isGotJSONObject(response);
					if(isGotJSONObject) {
						JSONArray jList = jManager.getJsonList();
						OrderedInfo orderedInfo;
						if(null == jList) {
							
						} else {
							JSONObject jObject;
							for(int i=0; i< jList.length(); i++) {
								jObject = getJSONObject(jList, i);
								if(null != jObject) {
									orderedInfo = new OrderedInfo();
									orderedInfo.setId(getString(jObject, "itemid"));
									orderedInfo.setName(getString(jObject, "title"));
									orderedInfo.setNum(StringUtils.parseInt(getString(jObject, "number")));
									orderedInfo.setPrice(getString(jObject, "price"));
									orderedInfo.setPriceTotal(getString(jObject, "amount"));
									orderedInfo.setImgURL(getString(jObject, "thumb"));

									orderedInfoList.add(orderedInfo);
								}
							}
						}
						int count = jManager.getResult();
						String msg = jManager.getMsg();
						System.out.println("------ 得到msg: " + count + ":" + msg);
					}
				}
				mHandler.sendEmptyMessage(MessageWhat.DIMISS_DIALOG);
			}
		}.start();
	}
	
	@Override
	public void notifyAdapter() {
		
	}
}
